/*
    Copyright: Octavian Marius Chincisan 2012.
    configuration file reader
*/
#include <assert.h>
#include <iostream>
#include <stdarg.h>
#include <limits.h>
#include "config.h"
#include <strutils.h>
#include "database.h"
//-----------------------------------------------------------------------------
Conf* GCFG;

//-----------------------------------------------------------------------------
Conf::Conf()
{
    //default values for max depth parsing links, and max links per site
    // are given for eah site if they are not specified in the site itsel.
    //site parameters overqrites these
    _depth=3;
    _links=1024;
    _pages=8;
}

//-----------------------------------------------------------------------------
Conf::~Conf()
{
}

void Conf::load(const char* cfgFile)
{
    string loco = cfgFile; loco += ".config";

    FILE* pf = _ttfopen(loco.c_str(), "rb");
    if(pf)
    {
        AutoCall<int (*)(FILE*), FILE*>    _a(::fclose, pf);
        char        pred[128];
        char        val[128];
        char        line[256];
        bool        inComment = false;
        try
        {
            while(!feof(pf))
            {

                if(fgets(line,255,pf))
                {
                    str_prepline(line);
                    if(*line==0)
                        continue;

                    if(inComment || *line=='#')
                        continue;

                    const char* pnext = str_ccpy(pred, line,'=');

                    if(*pred=='[')
                    {
                        str_lrtim(pred);
                        _section = pred;
                        continue;

                    }
                    else   if(*pred=='}')
                    {
                        _assign("}", " ", 0);
                    }
                    if(pnext && *pnext)
                    {
                        str_scpy(val, (char*)pnext+1, "#");

                        str_lrtim(val);
                        str_lrtim(pred);
                        _assign(pred, val, 0);
                    }

                }
                else
                    break;
                if(feof(pf))
                {
                    break;
                }
            }
        }
        catch(int& err)
        {
        }
    }
    else
    {
        printf( "Cannot find configuration file in curent folder\r\n");
        exit(0);
    }
}
//-----------------------------------------------------------------------------
#define BIND(mem_,name_)     _bind(lpred, #name_,  mem_.name_, val);

//-----------------------------------------------------------------------------
void Conf::_assign( const char* pred, const char* val, int line)
{
    char    lpred[256];
    char    loco[256];

    strcpy(loco,val);
    strcpy(lpred,pred);

    try
    {
        // read all the configuration values from mitza.config
        if(_section == "[global]")
        {
            BIND(_mitza,  threads);
            BIND(_mitza,     host);
            BIND(_mitza, database);
            BIND(_mitza,     user);
            BIND(_mitza, password);
            BIND(_mitza, blog);
            BIND(_mitza, pagelen);
            BIND(_mitza, sitefile);
            BIND(_mitza, depth);
        }
    }
    catch(int done) {

    }
    // the blog bits
    _blog = 0;
    _blog |= _mitza.blog.find('I') == string::npos ? 0 : 0x1;
    _blog |= _mitza.blog.find('W') == string::npos ? 0 : 0x2;
    _blog |= _mitza.blog.find('E') == string::npos ? 0 : 0x4;
    _blog |= _mitza.blog.find('T') == string::npos ? 0 : 0x8;
    _blog |= _mitza.blog.find('D') == string::npos ? 0 : 0x10;
    _blog |= _mitza.blog.find('X') == string::npos ? 0 : 0x20;


    // get from databse max_depth, and links/site.
/*
    // these settings are to be set from a web page
    mysqlcls        db;
    stringstream    s;

    db.connect(_mitza.host.c_str(),_mitza.user.c_str(),_mitza.password.c_str());
    db.qry("use mitza;");
    s << "SELECT * FROM config;";
    SR rezs(db.qry(s, true));
    size_t nrows = rezs->rows();
    assert(nrows==1);
    MYSQL_ROW r = rezs->fetch();
    _depth=::atoi(r[0]);
    _links=::atoi(r[1]);
    _pages=::atoi(r[2]);
    */

}


//-----------------------------------------------------------------------------
// not used.
void    Conf::check_log_size()
{

}


//-----------------------------------------------------------------------------
// not used
void Conf::rollup_logs(const char* rootName)
{
    char oldFn[256];
    char newFn[256];

    int iold = 40;
    sprintf(oldFn,"%s.log20",rootName);
    unlink(oldFn);

    while(iold-- > 0)
    {
        sprintf(newFn, "%s.log%d",rootName ,iold);
        sprintf(oldFn, "%s.log%d",rootName, iold-1);
        rename(oldFn, newFn);
    }
}
